package com.ly.elasticserach.controller;

import com.ass.elasticsearch.controller.EasyEsBaseController;
import com.ass.elasticsearch.page.EsPageOut;
import com.common.core.domain.AjaxResult;
import com.ly.elasticserach.empty.EasyEsTest;
import com.ly.elasticserach.input.EasyEsTestPageInput;
import com.ly.elasticserach.input.EasyEsTestSearchAfterInput;
import com.ly.elasticserach.service.EasyESTestService;
import io.swagger.annotations.ApiOperation;
import org.dromara.easyes.core.biz.EsPageInfo;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/easyEsCtrl")
public class EasyEsController extends EasyEsBaseController<EasyEsTest, EasyESTestService> {






    @PostMapping("/createIndex")
    @ApiOperation(value = "复杂场景索引创建",notes = "李勇")
    public AjaxResult createIndex(String indexName){
        return AjaxResult.success(defaultService.createIndex(indexName));
    }


    @PostMapping("/insert")
    @ApiOperation(value = "增数据",notes = "李勇")
    public AjaxResult insert(@RequestBody EasyEsTest model){
        model.setBookId(UUID.randomUUID().toString());
        model.setCreateDate(new Date());
        return AjaxResult.success(defaultService.insert(model));
    }


    @PostMapping("/insertBatch")
    @ApiOperation(value = "批量增数据",notes = "李勇")
    public AjaxResult insertBatch(){
        // 构造15条测试数据
        List<EasyEsTest> dataList = new ArrayList<>();
        Date now = new Date();

        String[] titles = {
            "诸葛亮三顾茅庐的故事",
            "刘备桃园三结义的典故",
            "关羽过五关斩六将的传奇",
            "张飞长坂坡喝退曹军的英勇",
            "赵云七进七出救阿斗的壮举",
            "马超割须弃袍的勇猛",
            "黄忠定军山斩夏侯渊的功绩",
            "周瑜赤壁之战火烧连营的智谋",
            "鲁肃联吴抗曹的战略",
            "孙权赤壁之战的关键决策",
            "曹操官渡之战以少胜多",
            "司马懿空城计的智慧",
            "吕布三英战吕布的勇武",
            "貂蝉闭月羞花的美貌",
            "华佗刮骨疗毒的医术"
        };

        String[] authors = {
            "罗贯中", "罗贯中", "罗贯中", "罗贯中", "罗贯中",
            "罗贯中", "罗贯中", "罗贯中", "罗贯中", "罗贯中",
            "罗贯中", "罗贯中", "罗贯中", "罗贯中", "罗贯中"
        };

        String[] contents = {
            "诸葛亮，字孔明，号卧龙，是三国时期蜀汉的丞相。他饱读诗书，精通兵法，被刘备三顾茅庐请出山，为蜀汉立下了汗马功劳。",
            "刘备、关羽、张飞在桃园中结为异姓兄弟，立下生死与共的誓言，共同为匡扶汉室而努力。",
            "关羽为了寻找刘备，过五关斩六将，展现了其忠义和勇武，成为后世传颂的英雄。",
            "张飞在长坂坡以一己之力喝退曹军，展现了其勇猛无畏的气概，为刘备争取了撤退的时间。",
            "赵云在长坂坡七进七出曹军，成功救出阿斗，展现了其非凡的武艺和忠诚。",
            "马超为了报父仇，与曹操展开激战，割须弃袍展现了他的勇猛和决心。",
            "黄忠虽然年迈，但在定军山一战中斩杀夏侯渊，展现了老当益壮的精神。",
            "周瑜在赤壁之战中用火攻之计，击败了曹操的强大水军，展现了其非凡的军事才能。",
            "鲁肃作为东吴的重要谋士，提出了联吴抗曹的战略，为赤壁之战的胜利奠定了基础。",
            "孙权在赤壁之战中做出关键决策，联合刘备共同抵抗曹操，展现了他的政治智慧。",
            "曹操在官渡之战中以少胜多，击败了袁绍，奠定了统一北方的基础。",
            "司马懿在面对诸葛亮的空城计时，展现了他的谨慎和智慧，避免了不必要的损失。",
            "吕布是三国时期最勇猛的武将之一，能够一人对战三位英雄而不落下风。",
            "貂蝉是古代四大美女之一，她的美貌足以让月亮都感到羞愧，让花朵都感到羞耻。",
            "华佗是三国时期的名医，他能够用刮骨疗毒的方法治疗疾病，展现了高超的医术。"
        };

        for (int i = 0; i < 15; i++) {
            EasyEsTest test = new EasyEsTest();
            test.setBookId(UUID.randomUUID().toString());
            test.setTitle(titles[i]);
            test.setAuthor(authors[i]);
            test.setContent(contents[i]);
            test.setCreateDate(now);
            dataList.add(test);
        }

        return AjaxResult.success(defaultService.insertBatch(dataList));
    }


    @ApiOperation(value = "测试查询加条件",notes = "李勇")
    @GetMapping("/list")
    public AjaxResult list(EasyEsTest model){
        List<EasyEsTest> list = defaultService.list(model);
        return AjaxResult.success(list);
    }


    @GetMapping("/oneById")
    @ApiOperation(value = "查一个",notes = "李勇")
    public AjaxResult oneById(String id){
        return AjaxResult.success(defaultService.oneById(id));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改",notes = "李勇")
    public AjaxResult update(@RequestBody EasyEsTest model){
        return AjaxResult.success(defaultService.updateById(model));
    }

    @DeleteMapping("/deleteById")
    @ApiOperation(value = "删除",notes = "李勇")
    public AjaxResult deleteById(String id){
        return AjaxResult.success(defaultService.deleteById(id));
    }


    @DeleteMapping("/deleteIndex")
    @ApiOperation(value = "删除索引",notes = "李勇")
    public AjaxResult deleteIndex(String indexName){
        return AjaxResult.success(defaultService.deleteIndex(indexName));
    }



    @GetMapping("/pageShallow")
    @ApiOperation(value = "浅分页 基于size+from的浅分页模式,适用于查询数据量少于1万的情况",notes = "李勇")
    public AjaxResult pageShallow(EasyEsTestPageInput input){
        //没引入@Mapper(componentModel = "spring") 先手动转下
        EasyEsTest model = new EasyEsTest();
        model.setTitle(input.getTitle());
        EsPageInfo<EasyEsTest> page = defaultService.page(input.getPage(), model);
        return AjaxResult.success(new EsPageOut<EasyEsTest>(page,page.getList()));
    }

    //滚动查询


    //使用searchAfter必须指定排序,若没有排序不仅会报错,
    // 而且对跳页也不友好. 需要保持searchAfter排序唯一,不然会导致分页失效,推荐使用id,uuid等进行排序.
    @GetMapping("/searchAfter")
    @ApiOperation(value = "searchAfter",notes = "李勇")
    public AjaxResult searchAfter(EasyEsTestSearchAfterInput input){
        //没引入@Mapper(componentModel = "spring") 先手动转下
        EasyEsTest model = new EasyEsTest();
        model.setTitle(input.getTitle());
        model.setAuthor(input.getAuthor());
        return AjaxResult.success(defaultService.searchAfter(input.getSAPageInfo(),model));
    }




























}
